package com.sqsf.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqsf.javabean.Permission;
import com.sqsf.mapper.PermissionMapper;
import com.sqsf.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    PermissionService permissionService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page,  @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        //通过定义参数获取请求的页数与每页条数
        //PageHelper提供静态方法startPage(页数，条数)
        //注意书写在service执行前
        PageHelper.startPage(page, limit);
        List<Permission> all = permissionService.findAll();
        //PageInfo代表页面详情 使用查询返回的数据创建
        //可以将pageInfo直接返回到页面进行使用
        PageInfo<Permission> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/permission/permission-list";
    }

    //添加数据
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Permission permission){
        permissionService.add(permission);
        //添加完毕后重新请求查询所有数据
        return "redirect:/permission/findAll";
    }


    //修改数据
    //先根据id查询对应的权限数据 跳转修改页面显示数据
    @RequestMapping(value = "/findById")
    public String findById(HttpServletRequest request,int id){
        Permission permission = permissionService.findById(id);
        request.setAttribute("permission",permission);
        return "/permission/permission-edit";
    }
    //提交修改数据
    @RequestMapping(value = "/edit")
    public String edit(Permission permission){
        permissionService.edit(permission);
        return "redirect:/permission/findAll";
    }

    //删除权限数据
    @RequestMapping(value = "/deleteById")
    public String deleteById(int id){
        permissionService.deleteById(id);
        return "redirect:/permission/findAll";
    }

}
